package hlhj.fhp.supreme.activitys

import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.db.UploadManager
import com.lzy.okgo.model.Response
import com.qiniu.android.http.ResponseInfo
import com.qiniu.android.storage.UpCompletionHandler
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.QNBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_ask_aty.*
import org.json.JSONObject
import java.io.File

class AskAty : BaseAty() {
    override fun getContentId(): Int {
        return R.layout.activity_ask_aty
    }
    private val PICK_Front: Int=1
    private val PICK_BACK: Int=2
    private val PICK_ZG: Int=3
    private var frontStr=""
    private var backStr=""
    private var zgStr=""
    private var qnImgToken=""
    override fun initView() {
        OkGo.post<QNBean>(Urls.BaseUrl+Urls.QNTOKEN)
                .params("type",0)
                .execute(object :JsonCallBack<QNBean>(this){
                    override fun onSuccess(response: Response<QNBean>?) {
                        val qnBean = response?.body()
                        if (qnBean?.code==1){
                            qnImgToken=qnBean.data.token
                        }else{
                            MyUtils.toast("获取七牛参数失败")
                        }
                    }
                })

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }

        btCommit.setOnClickListener {
            if (etCard.text.toString().isEmpty()
                    ||etInfo.text.toString().isEmpty()||etType.text.toString().isEmpty()
                    ||frontStr.equals("")||backStr.equals("")||zgStr.equals("")){
                if (frontStr.equals("")){
                    MyUtils.toast("身份证正面未上传")
                    return@setOnClickListener
                }
                if (backStr.equals("")){
                    MyUtils.toast("身份证反面未上传")
                    return@setOnClickListener
                }
                if (zgStr.equals("")){
                    MyUtils.toast("资格证未上传")
                    return@setOnClickListener
                }
                if (etCard.text.isEmpty()){
                    MyUtils.toast("类型未填写")
                    return@setOnClickListener
                }
                if (etInfo.text.isEmpty()){
                    MyUtils.toast("简介未填写")
                    return@setOnClickListener
                }

                if (etType.text.isEmpty()){
                    MyUtils.toast("课节未填写")
                    return@setOnClickListener
                }
                if (!btCheck.isChecked){
                    MyUtils.toast("请阅读用户协议")
                    return@setOnClickListener
                }
            }else{
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.ASKTEACHER)
                        .params("certificate",zgStr)
                        .params("style",etType.text.toString())
                        .params("idcard_face",frontStr)
                        .params("idcard_back",backStr)
                        .params("describe",etInfo.text.toString())
                        .execute(object :JsonCallBack<BaseBean>(this){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast("申请成功")
                                    finish()
                                }else{
                                    MyUtils.toast(bean?.msg)
                                }
                            }
                        })
            }
        }
        btRule.setOnClickListener {
            val intent = Intent(this, RuleAty::class.java)
            startActivity(intent)
        }
        btFront.setOnClickListener {
            Matisse.from(this)
                    .choose(MimeType.ofImage())
                    .maxSelectable(1)
                    .showSingleMediaType(true)
                    .countable(true)
                    .thumbnailScale(0.85f)
                    .theme(R.style.Matisse_Zhihu)
                    .imageEngine(GlideEngine())
                    .forResult(PICK_Front)
        }

        btBack.setOnClickListener {
            Matisse.from(this)
                    .choose(MimeType.ofImage())
                    .maxSelectable(1)
                    .showSingleMediaType(true)
                    .countable(true)
                    .thumbnailScale(0.85f)
                    .theme(R.style.Matisse_Zhihu)
                    .imageEngine(GlideEngine())
                    .forResult(PICK_BACK)
        }

        btZg.setOnClickListener {
            Matisse.from(this)
                    .choose(MimeType.ofImage())
                    .maxSelectable(1)
                    .showSingleMediaType(true)
                    .countable(true)
                    .thumbnailScale(0.85f)
                    .theme(R.style.Matisse_Zhihu)
                    .imageEngine(GlideEngine())
                    .forResult(PICK_ZG)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!= Activity.RESULT_OK)return
        when(requestCode){
            PICK_Front->{
                val pathResult = Matisse.obtainPathResult(data)
                val manager = com.qiniu.android.storage.UploadManager()

                manager.put(File(pathResult[0]),null,qnImgToken,object :UpCompletionHandler{
                    override fun complete(key: String?, info: ResponseInfo?, response: JSONObject?) {
                        val hash = response?.get("hash")
                        LogUtil.logi(hash.toString())
                        frontStr=hash.toString()
                    }
                },null)
                Glide.with(this@AskAty).load(pathResult[0]).asBitmap().into(imgFront)
                imgFront.visibility=View.VISIBLE
            }
            PICK_BACK->{
                val pathResult = Matisse.obtainPathResult(data)
                val manager = com.qiniu.android.storage.UploadManager()

                manager.put(File(pathResult[0]),null,qnImgToken,object :UpCompletionHandler{
                    override fun complete(key: String?, info: ResponseInfo?, response: JSONObject?) {
                        val hash = response?.get("hash")
                        LogUtil.logi(hash.toString())
                        backStr=hash.toString()
                    }
                },null)
                Glide.with(this@AskAty).load(pathResult[0]).asBitmap().into(imgBack)
                imgBack.visibility=View.VISIBLE
            }
            PICK_ZG->{
                val pathResult = Matisse.obtainPathResult(data)
                val manager = com.qiniu.android.storage.UploadManager()

                manager.put(File(pathResult[0]),null,qnImgToken,object :UpCompletionHandler{
                    override fun complete(key: String?, info: ResponseInfo?, response: JSONObject?) {
                        val hash = response?.get("hash")
                        LogUtil.logi(hash.toString())

                        zgStr=hash.toString()
                    }
                },null)
                Glide.with(this@AskAty).load(pathResult[0]).asBitmap().into(imgZg)
                imgZg.visibility=View.VISIBLE
            }
        }
    }
}
